/*
This file contains the second serial optimization.
1) The comparison to verify 1/A = 1/j+1/k+1/l was made faster by deferring casts to double 
and thus double arithmetic until the last step. 1/A = 1/(jkl)   simplifying the original 
equation to 1 = kl + jl + jk
*/

#include <stdio.h>
#include <math.h>
#include <omp.h>

int main(int argc, char *argv[]) {

    int i,j,k,l,aflag,limit,prod;
    double temp;

    if (argc<2) 
        return 0;

    limit = atoi(argv[1]);

	int stay = 1; 

    for (i=1;i<=limit;i++) 
    {
		stay = 1; 
        aflag=0;
        for (j=i;j>=-i && stay; j--) 
        {
            if (j==0) continue;
            for (k=i/j;k>=-i/j && stay; k--) 
            {
		        if (k==0) continue;
                for (l=i/(j*k);l<=-i/(j*k) && stay; l++) 
                {
			        if (j*k*l == i) 
                    {
						// This if statement contains a faster check 
						// than the original
                        if ((fabs(1-(double)(l*k+j*l+j*k)))<1e-9){
							aflag=1;
                        	stay = 0;	// This skips unnecessary iterations
						}
			        }
                }
            }
        }

        if (aflag) printf("%d\n",i);
    }
}


